package com.leoman.controller.api;

import com.leoman.core.ErrorType;
import com.leoman.core.bean.Result;
import com.leoman.entity.Mind;
import com.leoman.exception.NoDataException;
import com.leoman.exception.ParamErrorException;
import com.leoman.service.MindService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 心事api
 * Created by 涂奕恒 on 2016/11/24 0018.
 */
@Controller
@RequestMapping(value = "api/mind")
public class MindApi {

    @Autowired
    private MindService mindService;

    /**
     * @api {post} /api/mind/list 1、心事列表
     * @apiVersion 0.0.1
     * @apiName mind.list
     * @apiGroup mind
     *
     * @apiDescription 心事列表
     *
     * @apiParam   {Integer}   userId                     用户Id
     * @apiParam   {Integer}   isMe                       是否是我发的，0=否，1=是，默认为0
     * @apiParam   {Integer}   pageNum                    页码
     * @apiParam   {Integer}   pageSize                   每页显示条数
     *
     * @apiSuccess {Object}    list
     * @apiSuccess {Integer}   list.id                    心事ID
     * @apiSuccess {String}    list.title                 标题
     * @apiSuccess {String}    list.cover                 封面url
     * @apiSuccess {String}    list.synopsis              简介
     * @apiSuccess {String}    list.content               内容
     * @apiSuccess {String}    list.openMoney             偷心金额
     * @apiSuccess {Integer}   list.isTop                 是否置顶，0=否，1=是
     * @apiSuccess {Integer}   list.praiseNum             点赞数
     * @apiSuccess {Integer}   list.commentNum            评论数
     * @apiSuccess {Integer}   list.collectNum            收藏数
     * @apiSuccess {Integer}   list.isPraise              是否点赞，0=否，1=是
     * @apiSuccess {Integer}   list.isCollect             是否收藏，0=否，1=是
     * @apiSuccess {Integer}   list.isGet                 是否偷心，0=否，1=是
     * @apiSuccess {Integer}   list.isAdmin               是否管理员发送，0=否，1=是
     * @apiSuccess {Integer}   list.userId                发送人ID
     * @apiSuccess {String}    list.nickName              发送人昵称
     * @apiSuccess {String}    list.avatar                发送人头像
     * @apiSuccess {Integer}   list.regionId              发送人所在地ID
     * @apiSuccess {Integer}   list.regionInfo            发送人所在地名称
     * @apiSuccess {String}    list.createTime            发送时间
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "page": {
     *              "totalNum": 0,
     *              "totalPage": 0,
     *              "currentPage": 1
     *          },
     *          "list": [
     *              {
     *                  "id" : 1,
     *                  "title" : "大灰狼与小白兔的故事",
     *                  "cover" : "http://4.16.3.22:8080/images/cover.jpg",
     *                  "content" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草",
     *                  "openMoney" : 1.5,
     *                  "isTop" : 1,
     *                  "praiseNum" : 124,
     *                  "commentNum" : 20,
     *                  "collectNum" : 20,
     *                  "isPraise" : 0,
     *                  "isCollect" : 0,
     *                  "isGet" : 1,
     *                  "isAdmin" : 0,
     *                  "userId" : 1,
     *                  "nickName" : "张三",
     *                  "avatar" : "http://4.16.3.22:8080/images/cover.jpg",
     *                  "regionId" : 1,
     *                  "regionInfo" : "北京",
     *                  "createTime" : "1476843673494"
     *              }
     *          ]
     *      }
     * }
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public void list(HttpServletResponse response, Integer userId, Integer isMe, Integer pageNum, Integer pageSize) {
        Page<Mind> page = mindService.iPageForApp(userId, isMe, pageNum, pageSize);
        Map<String, Object> dataMap = JsonUtil.fitting(page);
        String result = JsonUtil.obj2ApiJson(Result.successApi(dataMap), "user", "imageList");
        WebUtil.printApi(response, result);
    }

    /**
     * @api {post} /api/mind/info 2、心事详情
     * @apiVersion 0.0.1
     * @apiName mind.info
     * @apiGroup mind
     *
     * @apiDescription 心事详情
     *
     * @apiParam   {Integer}   mindId                         心事Id                   （必填）
     * @apiParam   {Integer}   userId                         用户Id
     *
     * @apiSuccess {Object}    mindInfo
     * @apiSuccess {Integer}   mindInfo.id                    心事ID
     * @apiSuccess {String}    mindInfo.title                 标题
     * @apiSuccess {String}    mindInfo.cover                 封面url
     * @apiSuccess {String}    mindInfo.synopsis              简介
     * @apiSuccess {String}    mindInfo.content               内容
     * @apiSuccess {String}    mindInfo.imageList             图片列表
     * @apiSuccess {String}    mindInfo.imageList.id          图片id
     * @apiSuccess {String}    mindInfo.imageList.url         图片url
     * @apiSuccess {String}    mindInfo.imageList.createTime  图片创建时间
     * @apiSuccess {String}    mindInfo.openMoney             偷心金额
     * @apiSuccess {Integer}   mindInfo.isTop                 是否置顶，0=否，1=是
     * @apiSuccess {Integer}   mindInfo.praiseNum             点赞数
     * @apiSuccess {Integer}   mindInfo.commentNum            评论数
     * @apiSuccess {Integer}   mindInfo.collectNum            收藏数
     * @apiSuccess {Integer}   mindInfo.isPraise              是否点赞，0=否，1=是
     * @apiSuccess {Integer}   mindInfo.isCollect             是否收藏，0=否，1=是
     * @apiSuccess {Integer}   mindInfo.isGet                 是否偷心，0=否，1=是
     * @apiSuccess {Integer}   mindInfo.isAdmin               是否管理员发送，0=否，1=是
     * @apiSuccess {Integer}   mindInfo.userId                发送人ID
     * @apiSuccess {String}    mindInfo.nickName              发送人昵称
     * @apiSuccess {String}    mindInfo.avatar                发送人头像
     * @apiSuccess {Integer}   mindInfo.regionId              发送人所在地ID
     * @apiSuccess {String}    mindInfo.createTime            发送时间
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "mindInfo": {
     *              "id" : 1,
     *              "title" : "大灰狼与小白兔的故事",
     *              "cover" : "http://4.16.3.22:8080/images/cover.jpg",
     *              "content" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草",
     *              "imageList" : [
     *                  {
     *                      "id" : 1,
     *                      "url" : "http://4.16.3.22:8080/images/cover.jpg"
     *                  }
     *              ],
     *              "openMoney" : 1.5,
     *              "isTop" : 1,
     *              "praiseNum" : 124,
     *              "commentNum" : 20,
     *              "collectNum" : 10,
     *              "isPraise" : 0,
     *              "isCollect" : 0,
     *              "isGet" : 1,
     *              "isAdmin" : 0,
     *              "userId" : 1,
     *              "nickName" : "张三",
     *              "avatar" : "http://4.16.3.22:8080/images/cover.jpg",
     *              "regionId" : 1,
     *              "createTime" : "1476843673494"
     *          }
     *      }
     * }
     */
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public void info(HttpServletResponse response, Integer mindId, Integer userId) {
        try {
            Mind mind = mindService.iFindOneForApp(mindId, userId);
            String result = JsonUtil.obj2ApiJson(Result.successInfoApi(mind, "mindInfo"), "user");
            WebUtil.printApi(response, result);
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_NOT_PARAM));
        } catch (NoDataException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_NO_DATA));
        }
    }

    /**
     * @api {post} /api/mind/save 3、添加心事
     * @apiVersion 0.0.1
     * @apiName mind.save
     * @apiGroup mind
     *
     * @apiDescription 添加心事
     *
     * @apiParam   {Integer}   userId                用户Id                   （必填）
     * @apiParam   {Integer}   tempId                心事草稿Id
     * @apiParam   {String}    title                 标题                     （必填）
     * @apiParam   {String}    content               内容                     （必填）
     * @apiParam   {String}    openMoney             偷心金额                 （必填）
     * @apiParam   {Stream}    cover                 封面
     * @apiParam   {Integer}   imgNum                图片数量
     * @apiParam   {Stream}    image                 图片组
     * @apiParam   {Integer}   isDraft               是否草稿，0=否，1=是       （必填）
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{}
     * }
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(HttpServletResponse response, Integer tempId, Integer userId, String title, String content, String openMoney, Integer imgNum, Integer isDraft, MultipartRequest multipartRequest) {
        try {
            mindService.addMindInfo(false, tempId, userId, title, content, openMoney, imgNum, isDraft, multipartRequest);
            String result = JsonUtil.obj2ApiJson(Result.successApi());
            WebUtil.printApi(response, result);
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_NOT_PARAM));
        }
    }

    /**
     * @api {post} /api/mind/draft 4、心事草稿
     * @apiVersion 0.0.1
     * @apiName mind.draft
     * @apiGroup mind
     *
     * @apiDescription 心事草稿
     *
     * @apiParam   {Integer}   userId                         用户Id               （必填）
     *
     * @apiSuccess {Object}    mindInfo
     * @apiSuccess {Integer}   mindInfo.id                    心事ID
     * @apiSuccess {String}    mindInfo.title                 标题
     * @apiSuccess {String}    mindInfo.cover                 封面url
     * @apiSuccess {String}    mindInfo.content               内容
     * @apiSuccess {String}    mindInfo.imageList             图片列表
     * @apiSuccess {String}    mindInfo.imageList.id          图片id
     * @apiSuccess {String}    mindInfo.imageList.url         图片url
     * @apiSuccess {String}    mindInfo.imageList.createTime  图片创建时间
     * @apiSuccess {String}    mindInfo.openMoney             偷心金额
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "mindInfo": {
     *              "id" : 1,
     *              "title" : "标题",
     *              "cover" : "http://www.qq.com",
     *              "content" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草",
     *              "imageList" : [
     *                  {
     *                      "id" : 1,
     *                      "url" : "http://www.qq.com",
     *                      "createTime" : "14956465135461"
     *                  }
     *              ],
     *              "openMoney" : 1.5
     *          }
     *      }
     * }
     */
    @RequestMapping(value = "/draft", method = RequestMethod.POST)
    public void draft(HttpServletResponse response, Integer userId) {
        try {
            Mind mind = mindService.iFindDraftByUserId(userId);
            String result = JsonUtil.obj2ApiJson(Result.successInfoApi(mind, "mindInfo"), "user", "regionId", "isTop", "praiseNum", "commentNum", "isPraise", "isCollect", "isAdmin", "userId", "nickName", "avatar", "createTIme");
            WebUtil.printApi(response, result);
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, JsonUtil.obj2ApiJson(Result.failureApi(ErrorType.ERROR_NOT_PARAM)));
        } catch (NoDataException e) {
            WebUtil.printApi(response, JsonUtil.obj2ApiJson(Result.failureApi(ErrorType.ERROR_NO_DATA)));
        }
    }

    /**
     * @api {post} /api/mind/get 5、偷听心事
     * @apiVersion 0.0.1
     * @apiName mind.get
     * @apiGroup mind
     *
     * @apiDescription 偷听心事
     *
     * @apiParam   {Integer}   userId                用户Id                   （必填）
     * @apiParam   {Integer}   mindId                心事Id                   （必填）
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "userMindId" : 1
     *      }
     * }
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public void get(HttpServletResponse response, Integer mindId, Integer userId) {
        try {
            Integer num = mindService.getMindInfo(userId, mindId);
            String result = JsonUtil.obj2ApiJson(Result.successInfoApi(num, "userMindId"));
            System.out.println();
            WebUtil.printApi(response, result);
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_NOT_PARAM));
        }
    }

    /**
     * @api {post} /api/mind/openList 6、我的偷听心事列表
     * @apiVersion 0.0.1
     * @apiName mind.openList
     * @apiGroup mind
     *
     * @apiDescription 我的偷听心事列表
     *
     * @apiParam   {Integer}   userId                     用户Id
     * @apiParam   {Integer}   pageNum                    页码
     * @apiParam   {Integer}   pageSize                   每页显示条数
     *
     * @apiSuccess {Object}    list
     * @apiSuccess {Integer}   list.id                    心事ID
     * @apiSuccess {String}    list.title                 标题
     * @apiSuccess {String}    list.cover                 封面url
     * @apiSuccess {String}    list.content               内容
     * @apiSuccess {String}    list.openMoney             偷心金额
     * @apiSuccess {Integer}   list.isTop                 是否置顶，0=否，1=是
     * @apiSuccess {Integer}   list.praiseNum             点赞数
     * @apiSuccess {Integer}   list.commentNum            评论数
     * @apiSuccess {Integer}   list.collectNum            收藏数
     * @apiSuccess {Integer}   list.isPraise              是否点赞，0=否，1=是
     * @apiSuccess {Integer}   list.isCollect             是否收藏，0=否，1=是
     * @apiSuccess {Integer}   list.isGet                 是否偷心，0=否，1=是
     * @apiSuccess {Integer}   list.isAdmin               是否管理员发送，0=否，1=是
     * @apiSuccess {Integer}   list.userId                发送人ID
     * @apiSuccess {String}    list.nickName              发送人昵称
     * @apiSuccess {String}    list.avatar                发送人头像
     * @apiSuccess {Integer}   list.regionId              发送人所在地ID
     * @apiSuccess {String}    list.regionInfo            发送人所在地名称
     * @apiSuccess {String}    list.createTime            发送时间
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "page": {
     *              "totalNum": 0,
     *              "totalPage": 0,
     *              "currentPage": 1
     *          },
     *          "list": [
     *              {
     *                  "id" : 1,
     *                  "title" : "大灰狼与小白兔的故事",
     *                  "cover" : "http://4.16.3.22:8080/images/cover.jpg",
     *                  "content" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草",
     *                  "openMoney" : 1.5,
     *                  "isTop" : 1,
     *                  "praiseNum" : 124,
     *                  "commentNum" : 20,
     *                  "collectNum" : 20,
     *                  "isPraise" : 0,
     *                  "isCollect" : 0,
     *                  "isGet" : 1,
     *                  "isAdmin" : 0,
     *                  "userId" : 1,
     *                  "nickName" : "张三",
     *                  "avatar" : "http://4.16.3.22:8080/images/cover.jpg",
     *                  "regionId" : 1,
     *                  "regionInfo" : "北京",
     *                  "createTime" : "1476843673494"
     *              }
     *          ]
     *      }
     * }
     */
    @RequestMapping(value = "/openList", method = RequestMethod.POST)
    public void openList(HttpServletResponse response, Integer userId, Integer pageNum, Integer pageSize) {
        Page<Mind> page = mindService.iPageForAppOpen(userId, pageNum, pageSize);
        Map<String, Object> dataMap = JsonUtil.fitting(page);
        String result = JsonUtil.obj2ApiJson(Result.successApi(dataMap), "user", "imageList");
        WebUtil.printApi(response, result);
    }
}
